package android.slc.extras.system.domain

import android.Manifest
import android.app.AlertDialog
import android.content.DialogInterface
import android.slc.appbase.R
import android.slc.appbase.config.ConstantsBase
import android.slc.appbase.ui.vd.BaseAppVd
import android.slc.popup.SlcPopup2
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.IntentUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions

class StartupPermissionsVd: BaseAppVd<StartupPermissionsVmBox>() {

    override fun initMustCondition() {
        super.initMustCondition()
        vmBox.requestPermissions.observe(lifecycleOwner) { permissionArray: Array<String> ->
            XXPermissions.with(ActivityUtils.getTopActivity())
                .permission(*ConstantsBase.Permission.getBasePermissionArray())
                .request(object : OnPermissionCallback {
                    private var laxDenied = true
                    override fun onGranted(permissions: List<String>, allGranted: Boolean) {
                        if (allGranted || laxDenied) {
                            //全部允许或宽松拒绝
                            vmBox.setAllPermissionsGranted()
                        }
                    }

                    override fun onDenied(permissions: List<String>, doNotAskAgain: Boolean) {
                        super<OnPermissionCallback>.onDenied(permissions, doNotAskAgain)
                        laxDenied = permissions.size == 1 && Manifest.permission.ACCESS_FINE_LOCATION == permissions[0]
                        if (laxDenied) {
                            return
                        }
                        if (doNotAskAgain) {
                            SlcPopup2.createAlertDialogOperate().show(
                                AlertDialog.Builder(context)
                                    .setMessage(R.string.label_permission_base_setting_hint)
                                    .setCancelable(false)
                                    .setPositiveButton(
                                        R.string.action_i_know,
                                        DialogInterface.OnClickListener { dialog, which ->
                                            vmBox.resetNormalRequestPermissionStatus()
                                            ActivityUtils.startActivity(
                                                IntentUtils.getLaunchAppDetailsSettingsIntent(
                                                    AppUtils.getAppPackageName()
                                                )
                                            )
                                        })
                                    .create()
                            )
                            return
                        }
                        SlcPopup2.createAlertDialogOperate().show(
                            AlertDialog.Builder(context)
                                .setMessage(R.string.label_permission_base_hint)
                                .setCancelable(false)
                                .setPositiveButton(
                                    R.string.action_i_know,
                                    DialogInterface.OnClickListener { dialog, which ->
                                        vmBox.resetNormalRequestPermissionStatus()
                                        vmBox.checkPermission()
                                    })
                                .create()
                        )
                    }
                })
        }
    }

    /**
     * 再次检查权限
     */
    fun againCheckPermission(){
        vmBox.checkPermission()
    }
}